package ui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.ck.hzq.R;

import java.util.List;

import javax.inject.Inject;

import Bean.MPOSBean;
import base.LazyFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.loadingviewfinal.HeaderAndFooterRecyclerViewAdapter;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import coom.Constants;
import event.NetErrorEvent;
import ui.activity.home.MatterAct;
import ui.activity.home.biz.PickMachineBiz;
import ui.activity.home.component.DaggerPickMachineComponent;
import ui.activity.home.contract.PickMachineContract;
import ui.activity.home.module.PickMachineModule;
import ui.activity.home.presenter.PickMachinePresenter;
import ui.adapter.MPosAdapter;

/**
 * Created by chenjian on 18/2/21.
 * <p>
 * MPOS 列表
 */

public class MPOSFra extends LazyFragment implements PickMachineContract.View {


    @Inject
    PickMachinePresenter presenter;
    @Inject
    PickMachineBiz biz;


    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    MPosAdapter adp;
    private SharedPreferences sp;

    private boolean isPrepared;// 界面否
    private boolean isALeady = true;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.pull_refresh_layout, container, false);
        ButterKnife.bind(this, view);
        DaggerPickMachineComponent.builder().pickMachineModule(new PickMachineModule(this)).build().inject(this);
        presenter.setBiz(biz);
        sp = getActivity().getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);

        rv.setOnLoadMoreListener(() -> {
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getPosList(true, Constants.modelType[1]);
            }
        });
        rv.setOnItemClickListener(new HeaderAndFooterRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, int position) {
                startActString(MatterAct.class, adp.getData().get(position).getId());
            }
        });

        isPrepared = true;
        return view;
    }

    @Override
    protected void lazyLoad() {
        if (isPrepared && isVisible && isALeady) {
            // TODO 在这里处理逻辑
            ptr.autoRefresh(true);
            isALeady = false;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.bind(getActivity()).unbind();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }


    @Override
    public void upDateUI(List<MPOSBean.ChildrenBean> bean) {
        if (adp == null && bean.size() > 0) {
            adp = new MPosAdapter(getActivity(), bean.get(0).getPosModels());
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }


}
